home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / xxmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  4.9 KB  |  228 lines

  1. /* Use this file as a template to start implementing a module that
  2.    also declares objects types. All occurrences of 'Xxo' should be changed
  3.    to something reasonable for your objects. After that, all other
  4.    occurrences of 'xx' should be changed to something reasonable for your
  5.    module. If your module is named foo your sourcefile should be named
  6.    foomodule.c.
  7.    
  8.    You will probably want to delete all references to 'x_attr' and add
  9.    your own types of attributes instead.  Maybe you want to name your
  10.    local variables other than 'self'.  If your object type is needed in
  11.    other files, you'll have to create a file "foobarobject.h"; see
  12.    intobject.h for an example. */
  13.  
  14. /* Xxo objects */
  15.  
  16. #include "Python.h"
  17.  
  18. static PyObject *ErrorObject;
  19.  
  20. typedef struct {
  21.     PyObject_HEAD
  22.     PyObject    *x_attr;    /* Attributes dictionary */
  23. } XxoObject;
  24.  
  25. staticforward PyTypeObject Xxo_Type;
  26.  
  27. #define XxoObject_Check(v)    ((v)->ob_type == &Xxo_Type)
  28.  
  29. static XxoObject *
  30. newXxoObject(arg)
  31.     PyObject *arg;
  32. {
  33.     XxoObject *self;
  34.     self = PyObject_New(XxoObject, &Xxo_Type);
  35.     if (self == NULL)
  36.         return NULL;
  37.     self->x_attr = NULL;
  38.     return self;
  39. }
  40.  
  41. /* Xxo methods */
  42.  
  43. static void
  44. Xxo_dealloc(self)
  45.     XxoObject *self;
  46. {
  47.     Py_XDECREF(self->x_attr);
  48.     PyObject_Del(self);
  49. }
  50.  
  51. static PyObject *
  52. Xxo_demo(self, args)
  53.     XxoObject *self;
  54.     PyObject *args;
  55. {
  56.     if (!PyArg_ParseTuple(args, ":demo"))
  57.         return NULL;
  58.     Py_INCREF(Py_None);
  59.     return Py_None;
  60. }
  61.  
  62. static PyMethodDef Xxo_methods[] = {
  63.     {"demo",    (PyCFunction)Xxo_demo,    1},
  64.     {NULL,        NULL}        /* sentinel */
  65. };
  66.  
  67. static PyObject *
  68. Xxo_getattr(self, name)
  69.     XxoObject *self;
  70.     char *name;
  71. {
  72.     if (self->x_attr != NULL) {
  73.         PyObject *v = PyDict_GetItemString(self->x_attr, name);
  74.         if (v != NULL) {
  75.             Py_INCREF(v);
  76.             return v;
  77.         }
  78.     }
  79.     return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
  80. }
  81.  
  82. static int
  83. Xxo_setattr(self, name, v)
  84.     XxoObject *self;
  85.     char *name;
  86.     PyObject *v;
  87. {
  88.     if (self->x_attr == NULL) {
  89.         self->x_attr = PyDict_New();
  90.         if (self->x_attr == NULL)
  91.             return -1;
  92.     }
  93.     if (v == NULL) {
  94.         int rv = PyDict_DelItemString(self->x_attr, name);
  95.         if (rv < 0)
  96.             PyErr_SetString(PyExc_AttributeError,
  97.                     "delete non-existing Xxo attribute");
  98.         return rv;
  99.     }
  100.     else
  101.         return PyDict_SetItemString(self->x_attr, name, v);
  102. }
  103.  
  104. statichere PyTypeObject Xxo_Type = {
  105.     /* The ob_type field must be initialized in the module init function
  106.      * to be portable to Windows without using C++. */
  107.     PyObject_HEAD_INIT(NULL)
  108.     0,            /*ob_size*/
  109.     "Xxo",            /*tp_name*/
  110.     sizeof(XxoObject),    /*tp_basicsize*/
  111.     0,            /*tp_itemsize*/
  112.     /* methods */
  113.     (destructor)Xxo_dealloc, /*tp_dealloc*/
  114.     0,            /*tp_print*/
  115.     (getattrfunc)Xxo_getattr, /*tp_getattr*/
  116.     (setattrfunc)Xxo_setattr, /*tp_setattr*/
  117.     0,            /*tp_compare*/
  118.     0,            /*tp_repr*/
  119.     0,            /*tp_as_number*/
  120.     0,            /*tp_as_sequence*/
  121.     0,            /*tp_as_mapping*/
  122.     0,            /*tp_hash*/
  123. };
  124. /* --------------------------------------------------------------------- */
  125.  
  126. /* Function of two integers returning integer */
  127.  
  128. static PyObject *
  129. xx_foo(self, args)
  130.     PyObject *self; /* Not used */
  131.     PyObject *args;
  132. {
  133.     long i, j;
  134.     long res;
  135.     if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
  136.         return NULL;
  137.     res = i+j; /* XXX Do something here */
  138.     return PyInt_FromLong(res);
  139. }
  140.  
  141.  
  142. /* Function of no arguments returning new Xxo object */
  143.  
  144. static PyObject *
  145. xx_new(self, args)
  146.     PyObject *self; /* Not used */
  147.     PyObject *args;
  148. {
  149.     XxoObject *rv;
  150.     
  151.     if (!PyArg_ParseTuple(args, ":new"))
  152.         return NULL;
  153.     rv = newXxoObject(args);
  154.     if ( rv == NULL )
  155.         return NULL;
  156.     return (PyObject *)rv;
  157. }
  158.  
  159. /* Example with subtle bug from extensions manual ("Thin Ice"). */
  160.  
  161. static PyObject *
  162. xx_bug(self, args)
  163.     PyObject *self;
  164.     PyObject *args;
  165. {
  166.     PyObject *list, *item;
  167.     
  168.     if (!PyArg_ParseTuple(args, "O:bug", &list))
  169.         return NULL;
  170.     
  171.     item = PyList_GetItem(list, 0);
  172.     /* Py_INCREF(item); */
  173.     PyList_SetItem(list, 1, PyInt_FromLong(0L));
  174.     PyObject_Print(item, stdout, 0);
  175.     printf("\n");
  176.     /* Py_DECREF(item); */
  177.     
  178.     Py_INCREF(Py_None);
  179.     return Py_None;
  180. }
  181.  
  182. /* Test bad format character */
  183.  
  184. static PyObject *
  185. xx_roj(self, args)
  186.     PyObject *self; /* Not used */
  187.     PyObject *args;
  188. {
  189.     PyObject *a;
  190.     long b;
  191.     if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
  192.         return NULL;
  193.     Py_INCREF(Py_None);
  194.     return Py_None;
  195. }
  196.  
  197.  
  198. /* List of functions defined in the module */
  199.  
  200. static PyMethodDef xx_methods[] = {
  201.     {"roj",        xx_roj,        1},
  202.     {"foo",        xx_foo,        1},
  203.     {"new",        xx_new,        1},
  204.     {"bug",        xx_bug,        1},
  205.     {NULL,        NULL}        /* sentinel */
  206. };
  207.  
  208.  
  209. /* Initialization function for the module (*must* be called initxx) */
  210.  
  211. DL_EXPORT(void)
  212. initxx()
  213. {
  214.     PyObject *m, *d;
  215.  
  216.     /* Initialize the type of the new type object here; doing it here
  217.      * is required for portability to Windows without requiring C++. */
  218.     Xxo_Type.ob_type = &PyType_Type;
  219.  
  220.     /* Create the module and add the functions */
  221.     m = Py_InitModule("xx", xx_methods);
  222.  
  223.     /* Add some symbolic constants to the module */
  224.     d = PyModule_GetDict(m);
  225.     ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
  226.     PyDict_SetItemString(d, "error", ErrorObject);
  227. }
  228.